/*
 *The Team class is just a list of players. It also has a win loss record.
 */

#include "team.h"
#include <QString>
#include "dbconnect.h"
#include <QVariant>
#include <QDebug>
#include <QtSql/QSqlQuery>

using namespace std;

/*
 *  private variables
 *  char* name;
 *  Player* roster;
 *  int wins;
 *  int losses;
*/
Team::Team()
{
    //initialize from database data
}

Team::~Team() {
        // destroy
}

QString Team::getName(){
    return name;
}

void Team::setName(QString inputName){
    name=inputName;
}

int Team::getWins(){
    return wins;
}

void Team::setWins(int inputWins){
    wins= inputWins;
}

int Team::getLosses(){
    return losses;
}

void Team::setLosses(int inputLosses){
    losses=inputLosses;
}

void Team::getRoster(){

    DbConnect connection = DbConnect();

    QString query_string = QString("SELECT *, team_id FROM players AS P JOIN team_player_relation AS T ON P.u_id=T.player_id WHERE T.team_id = %1").arg(id);




    QSqlQuery query;
    query.prepare(query_string);
    query.exec();
    while(query.next()) {
        Player* temp_player = new Player();


        temp_player->setFirstName(query.value(0).toString());
        temp_player->setLastName(query.value(2).toString());
        temp_player->setJerseyNumber(query.value(3).toInt());
        temp_player->setId(query.value(4).toInt());
        roster.append(temp_player);
    }
    query.finish();


}

void Team::setId(int team_id) {

    id = team_id;
}

int Team::getID() {

    return id;

}

bool Team::hasPlayer(int player_id) {

    foreach(Player* p, roster) {

        if (p->getId() == player_id)
        return true;
    }
    return false;
}

void Team::debugRoster() {


    foreach(Player* pl, roster) {
        QString temp = QString("Dziala");
        QString dupa = pl->getFirstName();
        qDebug() << "Player roster: " << dupa << "ID " << pl->getId();
    }
}

void Team::updateRoster(QList<int> add, QList<int> rem) {

    foreach(int added, add) {
        qDebug() << "INT " << added;

        QString query_string = QString("Insert into team_player_relation VALUES('%1','%2')").arg(id).arg(added);
        qDebug() << query_string;
        QSqlQuery query(query_string);
        query.finish();
    }

    foreach(int removed, rem) {


        QString query_string = QString("Delete from team_player_relation WHERE team_id = %1 AND player_id = %2").arg(id).arg(removed);
        qDebug() << query_string;
        QSqlQuery query(query_string);
        query.finish();
    }
}

void Team::ListToVector() {

    foreach(Player* player, roster) {

        team_players.append(player);

    }


}

QVector<Player*> Team::getPlayers() {

    return team_players;
}

QList<Player*> Team::returnRoster() {
    return roster;
}


